package io.gitee.javalaoniu.mpgen.genarator;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class MybatisPlusGeneratorProperties {

    /**
     * 生成的代码是否包含swagger
     */
    private boolean withSwagger;
    /**
     * 生成的代码是否使用useLombok
     */
    private boolean useLombok;
    /**
     * 数据库地址
     */
    private String jdbcUrl;
    /**
     * 数据库账号
     */
    private String jdbcUser;

    /**
     * 数据库密码
     */
    private String jdbcPwd;

    /**
     * 数据库驱动类
     */
    private String driverClass;

    /**
     * 模式，缺省为空。
     * 如postgresql中可以理解为库名，mysql中也可以理解为库名。
     * 有些sql中需要schema.tablename才能查询。
     */
    private String schema;

    /**
     * 表名前缀，生成类就不会包含前缀。如果表名中不带前缀就不会起作用
     */
    private String tablePrefix;

    /**
     * 表名，多个表以英文逗号分隔
     */
    private String tableNames;

    /**
     * 生成代码保存根目录
     */
    private String codeSavePath;

    /**
     * 生成代码中注释显示的作者
     */
    private String author;

    /**
     * 包名配置：包名前缀
     */
    private String packagePrefix;

    /**
     * 包名配置：模块名， 如"core"，包路径拼接为："com.example.core"
     */
    private String packageModuleName;

    /**
     * 包名配置：Entity包名，如"model.po"，包路径拼接为："com.example.core.model.po"
     */
    private String entityPackageName;

    /**
     * 包名配置：Service包名，如"service"，包路径拼接为："com.example.core.service"
     */
    private String servicePackageName;

    /**
     * 包名配置：Service Impl包名，如"service.impl"，包路径拼接为："com.example.core.service.impl"
     */
    private String serviceImplPackageName;

    /**
     * 包名配置：Mapper包名、Mapper XML包名，如"mapper"，包路径拼接为："com.example.core.mapper"
     */
    private String mapperPackageName;

    /**
     * Mapper XML保存路径，如mapperXmlPath="mapper"，则最终保存路径为：codeSavePath + "/src/main/resources/" + mapperXmlPath + "/"
     */
    private String mapperXmlPath;

    /**
     * 包名配置：Controller包名，如"controller"，包路径拼接为："com.example.core.controller"
     */
    private String controllerPackageName;

    /**
     * controller的父类（带包路径）， 缺省为空
     */
    private String superControllerClass;

    /**
     * entity的父类（带包路径）， 缺省为空
     */
    private String superEntityClass;

    /**
     * 接口地址前缀，如"/api/v1"
     */
    private String requestMappingUrlPrefix;

    private MybatisPlusGeneratorProperties(Builder builder) {
        setWithSwagger(builder.withSwagger);
        setUseLombok(builder.useLombok);
        setJdbcUrl(builder.jdbcUrl);
        setJdbcUser(builder.jdbcUser);
        setJdbcPwd(builder.jdbcPwd);
        setDriverClass(builder.driverClass);
        setSchema(builder.schema);
        setTablePrefix(builder.tablePrefix);
        setTableNames(builder.tableNames);
        setCodeSavePath(builder.codeSavePath);
        setAuthor(builder.author);
        setPackagePrefix(builder.packagePrefix);
        setPackageModuleName(builder.packageModuleName);
        setEntityPackageName(builder.entityPackageName);
        setServicePackageName(builder.servicePackageName);
        setServiceImplPackageName(builder.serviceImplPackageName);
        setMapperPackageName(builder.mapperPackageName);
        setMapperXmlPath(builder.mapperXmlPath);
        setControllerPackageName(builder.controllerPackageName);
        setSuperControllerClass(builder.superControllerClass);
        setSuperEntityClass(builder.superEntityClass);
        setRequestMappingUrlPrefix(builder.requestMappingUrlPrefix);
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public static final class Builder {
        private boolean withSwagger;
        private boolean useLombok;
        private String jdbcUrl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&tinyInt1isBit=false";
        private String jdbcUser = "root";
        private String jdbcPwd = "root";
        private String driverClass = "com.mysql.cj.jdbc.Driver";
        private String schema;
        private String tablePrefix = "t_";
        private String tableNames = "test1,test2";
        private String codeSavePath;
        private String author = "javalaoniu";
        private String packagePrefix = "io.gitee.javalaoniu";
        private String packageModuleName = "demo";
        private String controllerPackageName = "controller";
        private String servicePackageName = "service";
        private String serviceImplPackageName = "service.impl";
        private String entityPackageName = "entity";
        private String mapperPackageName = "mapper";
        private String mapperXmlPath = "/mapper";
        private String superControllerClass;
        private String superEntityClass;
        private String requestMappingUrlPrefix = "/api/v1";

        private Builder() {
        }

        public Builder withSwagger(boolean withSwagger) {
            this.withSwagger = withSwagger;
            return this;
        }

        public Builder useLombok(boolean useLombok) {
            this.useLombok = useLombok;
            return this;
        }

        public Builder jdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
            return this;
        }

        public Builder jdbcUser(String jdbcUser) {
            this.jdbcUser = jdbcUser;
            return this;
        }

        public Builder jdbcPwd(String jdbcPwd) {
            this.jdbcPwd = jdbcPwd;
            return this;
        }

        public Builder driverClass(String driverClass) {
            this.driverClass = driverClass;
            return this;
        }

        public Builder schema(String schema) {
            this.schema = schema;
            return this;
        }

        public Builder tablePrefix(String tablePrefix) {
            this.tablePrefix = tablePrefix;
            return this;
        }

        public Builder tableNames(String tableNames) {
            this.tableNames = tableNames;
            return this;
        }

        public Builder codeSavePath(String codeSavePath) {
            this.codeSavePath = codeSavePath;
            return this;
        }

        public Builder author(String author) {
            this.author = author;
            return this;
        }

        public Builder packagePrefix(String packagePrefix) {
            this.packagePrefix = packagePrefix;
            return this;
        }

        public Builder packageModuleName(String packageModuleName) {
            this.packageModuleName = packageModuleName;
            return this;
        }

        public Builder entityPackageName(String entityPackageName) {
            this.entityPackageName = entityPackageName;
            return this;
        }

        public Builder servicePackageName(String servicePackageName) {
            this.servicePackageName = servicePackageName;
            return this;
        }

        public Builder serviceImplPackageName(String serviceImplPackageName) {
            this.serviceImplPackageName = serviceImplPackageName;
            return this;
        }

        public Builder mapperPackageName(String mapperPackageName) {
            this.mapperPackageName = mapperPackageName;
            return this;
        }

        public Builder mapperXmlPath(String mapperXmlPath) {
            this.mapperXmlPath = mapperXmlPath;
            return this;
        }

        public Builder controllerPackageName(String controllerPackageName) {
            this.controllerPackageName = controllerPackageName;
            return this;
        }

        public Builder superControllerClass(String superControllerClass) {
            this.superControllerClass = superControllerClass;
            return this;
        }

        public Builder superEntityClass(String superEntityClass) {
            this.superEntityClass = superEntityClass;
            return this;
        }

        public Builder requestMappingUrlPrefix(String requestMappingUrlPrefix) {
            this.requestMappingUrlPrefix = requestMappingUrlPrefix;
            return this;
        }

        public MybatisPlusGeneratorProperties build() {
            if (codeSavePath == null) {
                codeSavePath = System.getProperty("user.dir");
                System.out.println("code save path: " + codeSavePath);
            }

            return new MybatisPlusGeneratorProperties(this);
        }
    }
}
